package com.zhn.evsi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhn.evsi.domain.dto.evsi.EvsiIndustryActivityMaterialsDTO;
import com.zhn.evsi.domain.dto.evsi.EvsiIndustryActivityMaterialsInfoDTO;
import com.zhn.evsi.domain.entity.evsi.EvsiIndustryActivityMaterials;
import com.zhn.evsi.domain.entity.evsi.EvsiIndustryActivityProducts;
import com.zhn.evsi.domain.entity.evsi.EvsiInvtypes;
import com.zhn.evsi.mapper.EvsiIndustryActivityMaterialsMapper;
import com.zhn.evsi.service.IEvsiIndustryActivityMaterialsService;
import com.zhn.evsi.service.IEvsiIndustryActivityProductsService;
import com.zhn.evsi.service.IEvsiInvtypesService;
import com.zhn.evsi.service.IEvsiPlanetSchematicStypeMapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class EvsiIndustryActivityMaterialsServiceImpl extends ServiceImpl<EvsiIndustryActivityMaterialsMapper, EvsiIndustryActivityMaterials> implements IEvsiIndustryActivityMaterialsService {

    @Autowired
    private IEvsiPlanetSchematicStypeMapService planetSchematicStypeMapService;

    @Autowired
    private IEvsiIndustryActivityProductsService industryActivityProductsService;

    @Autowired
    private IEvsiInvtypesService invtypesService;

    // 蓝图制造
    @Override
    public EvsiIndustryActivityMaterialsDTO bluePrint(Integer typeId, Integer quantity) {

        if (quantity == null && quantity == 0) {
            quantity = 1;
        }

        EvsiInvtypes invtypes = invtypesService.getOne(new LambdaQueryWrapper<EvsiInvtypes>().eq(EvsiInvtypes::getTypeId, typeId));

        EvsiIndustryActivityMaterialsDTO build = EvsiIndustryActivityMaterialsDTO.builder()
                .typeName(invtypes.getTypeName())
                .blueprintTypeId(typeId)
                .materialList(getMaterialList(typeId, quantity))
                .quantity(quantity)
                .build();
        return build;
    }

    private List<EvsiIndustryActivityMaterialsInfoDTO> getMaterialList(Integer typeId, Integer quantity) {
        List<EvsiIndustryActivityMaterialsInfoDTO> industryActivityMaterialsInfoDTOS = new ArrayList<>();
        List<EvsiIndustryActivityMaterials> industryActivityMaterials = this.list(new LambdaQueryWrapper<EvsiIndustryActivityMaterials>().eq(EvsiIndustryActivityMaterials::getBlueprintTypeId, typeId));
        industryActivityMaterials.forEach(v -> {
            EvsiIndustryActivityMaterialsInfoDTO evsiIndustryActivityMaterialsInfoDTO = new EvsiIndustryActivityMaterialsInfoDTO();
            EvsiInvtypes invtypes = invtypesService.getOne(new LambdaQueryWrapper<EvsiInvtypes>().eq(EvsiInvtypes::getTypeId, v.getMaterialTypeId()));

            EvsiIndustryActivityProducts industryActivityProducts = industryActivityProductsService
                    .getOne(new LambdaQueryWrapper<EvsiIndustryActivityProducts>()
                    .eq(EvsiIndustryActivityProducts::getProductTypeId, v.getMaterialTypeId()));

            evsiIndustryActivityMaterialsInfoDTO.setMaterialQuantity(v.getQuantity() * quantity);
            evsiIndustryActivityMaterialsInfoDTO.setMaterialTypeId(v.getMaterialTypeId());
            evsiIndustryActivityMaterialsInfoDTO.setMaterialTypeName(invtypes.getTypeName());
            if (industryActivityProducts != null) {
                evsiIndustryActivityMaterialsInfoDTO.setMaterialBluePrintTypeId(industryActivityProducts.getBlueprintTypeId());
                evsiIndustryActivityMaterialsInfoDTO.setMaterials(bluePrint(industryActivityProducts.getBlueprintTypeId(), v.getQuantity() * quantity));
            }
            industryActivityMaterialsInfoDTOS.add(evsiIndustryActivityMaterialsInfoDTO);
        });
        return industryActivityMaterialsInfoDTOS;
    }
}
